package com.school.service.impl;

import com.school.dao.PicsMapper;
import com.school.model.Pics;
import com.school.model.PicsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class PicServiceImpl {
    @Resource
    private PicsMapper picsMapper;
    @Autowired
    private UserServiceImpl userService;
    @Value("${file.path}")
    private String filePath;

    @Resource

    public List<Pics> querySelective(Integer userId, int value) {
        PicsExample picsExample = new PicsExample();
        PicsExample.Criteria criteria = picsExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTypeEqualTo(value);
        criteria.andDeletedEqualTo(false);
        return this.picsMapper.selectByExampleSelective(picsExample);
    }

    private String getRandomFileName(String fileName) {
        String format = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        UUID uuid = UUID.randomUUID();
        return uuid + "." + format;
    }

    public void store(MultipartFile file, String location) throws IOException {
        File fileInServer = new File(this.filePath + location);
        file.transferTo(fileInServer);
    }

    public Pics upload(Integer userId, int type, MultipartFile file) throws IOException {
        List<Pics> pics = this.querySelective(userId, type);
        String location = this.getRandomFileName((String) Objects.requireNonNull(file.getOriginalFilename()));
        this.store(file, location);
        if (pics.size() != 0) {
            Pics pics1 = (Pics) pics.get(0);
            pics1.setLocation(location);
            updateByIdSelective(pics1);
            return querySelective(userId, type).get(0);
        } else {
            Pics p = new Pics();
            p.setLocation(location);
            p.setType(type);
            p.setUserId(userId);
            this.add(p);
            List<Pics> pics1 = this.querySelective(userId, type);
            return (Pics) pics1.get(0);
        }
    }

    private void add(Pics pics) {
        pics.setAddTime(LocalDateTime.now());
        pics.setUpdateTime(LocalDateTime.now());
        this.picsMapper.insertSelective(pics);
    }

    private void updateByIdSelective(Pics pics) {
        PicsExample picsExample = new PicsExample();
        PicsExample.Criteria criteria = picsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        pics.setUpdateTime(LocalDateTime.now());
        picsMapper.updateByPrimaryKeySelective(pics);
    }

    private Pics queryById(Integer id) {
        Pics pics = picsMapper.selectByPrimaryKey(id);
        return pics;
    }
}
